package com.cn.bulu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.bulu.dao.PriceDao;
import com.cn.bulu.pojo.Composition;
import com.cn.bulu.pojo.Goods;
import com.cn.bulu.pojo.Sample;
import com.cn.bulu.service.PriceService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.ClothCurtain.Arith;
import com.cn.bulu.utils.ClothCurtain.Calculate;
import com.cn.bulu.utils.finishShadeUtils.FinishShadePrice;
import com.cn.bulu.utils.finishShadeUtils.FinishShadePriceUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service("priceService")
public class PriceServiceImpl implements PriceService {

	private static Logger logger = Logger.getLogger(PriceServiceImpl.class);

	@Resource
	private PriceDao priceDao;

	/**
	 * 成品帘价格计算 finishList={sampleId:,finish:[{wide:,high:},{}]}
	 */
	public BuluResult getPriceOfFinish(String finishList){
		JSONObject fin = JSON.parseObject(finishList);
		Integer sampleId = fin.getInteger("sampleId");
		// 单价
		Sample sample = priceDao.getSampleBySampleId(sampleId);
		DecimalFormat df = new DecimalFormat("0.00");
		Double money = 0.0;
		if (sample.getFptId() != 2) {
			// 这是其他类型的
			JSONArray finArray = JSONArray.parseArray(fin.getString("finish"));
			Double area = 0.0;
			for (int i = 0; i < finArray.size(); i++) {
				JSONObject highAndWide = JSONObject.parseObject(finArray.get(i).toString());
				area += highAndWide.getDouble("wide") * highAndWide.getDouble("high");
			}
			if (area < 1.0) {
				area = 1.0;
			} else {
				area = Double.parseDouble(df.format(area));
			}
			money = area * sample.getPrice();
		} else {
			JSONArray finArray = JSONArray.parseArray(fin.getString("finish"));
			// 宽和高的集合
			List<FinishShadePrice> overDoorList = new ArrayList<>();
			List<FinishShadePrice> underDoorList = new ArrayList<>();
			// 得到门幅
			List<Double> doors = priceDao.getDoorsBySampleId(sampleId);
			for (int i = 0; i < finArray.size(); i++) {
				JSONObject highAndWide = JSONObject.parseObject(finArray.get(i).toString());
				FinishShadePrice finishShadePrice = new FinishShadePrice();
				finishShadePrice.setWide(highAndWide.getDouble("wide"));
				finishShadePrice.setHigh(highAndWide.getDouble("high"));
				// 高和门幅做对比,如果是
				if (highAndWide.getDouble("high") > Collections.max(doors)) {
					// 这是高高于所有门幅
					overDoorList.add(finishShadePrice);
				} else {
					// 这个高低于门幅
					underDoorList.add(finishShadePrice);
				}
			}
			// 高高于门幅
			Double overArea = 0.0;
			List<FinishShadePrice> over = FinishShadePriceUtils.getDosageOfFinishShade(doors, overDoorList);
			for (FinishShadePrice finishShadePrice : over) {
				if (finishShadePrice.getDoorWidth() != null && finishShadePrice.getHigh() != null) {
					overArea = overArea + (finishShadePrice.getDoorWidth()) * (finishShadePrice.getHigh());
				}
			}

			// 高低于门幅
			Double belowArea = 0.0;
			List<FinishShadePrice> below = FinishShadePriceUtils.getDosageOfFinishBelowDoor(doors, underDoorList);
			for (FinishShadePrice finishShadePrice : below) {
				if (finishShadePrice.getDoorWidth() != null && finishShadePrice.getWide() != null) {
					belowArea = belowArea + (finishShadePrice.getDoorWidth()) * (finishShadePrice.getWide());
				}
			}
			money = Arith.mul(Double.parseDouble(df.format(overArea + belowArea)) , sample.getPrice());

		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(money);//之前是buluResult.setData(Math.round(money));做了四舍五入操作

		return buluResult;
	}

	/**
	 * 计算布艺帘价格
	 */
	public BuluResult curtainPrice(Goods goods) {
		//商品的倍折，times_fold
		//折边，hem
		//打孔价格，holePrice
		//韩折价格 zhePrice
		BuluResult buluResult = calcCurtainPrice(goods);
		if (buluResult.getStatus() == 0) {
			Goods tempGoods = (Goods) buluResult.getData();
			buluResult.setData(tempGoods.getGoodsPrice());
		}
		return buluResult;
	}
	
	
	
	
	public BuluResult calcCurtainPrice(Goods goods) {
		BuluResult buluResult = new BuluResult();
		Map<String, Object> beizhe = priceDao.selectBeizhe(goods.getSampleId());
		if(beizhe.size()==0){
			buluResult.setStatus(1);
			buluResult.setMessage("查询数据为空");
			return buluResult;
		}
		switch (goods.getStructure()) {
			case 0:// 单布
					Double sumPrice = 0.0;
					//主布属性
					List<Composition> list = priceDao.selectZhubu(goods.getSampleId());
						if(list.size()==0){
							buluResult.setStatus(1);
							buluResult.setMessage("主布属性查询结果为空");
							return buluResult;
						}
						switch (goods.getStyle()) {
						case 0:// 打孔
							goods.setShadeHead(0);   //无帘头
							list.get(0).setPrice((Double) beizhe.get("holePrice"));
							break;
						case 1:// 韩折
							goods.setShadeHead(0);   //无帘头
							list.get(0).setPrice((Double) beizhe.get("zhePrice"));
							break;
						case 2:// 韩折有帘头
							goods.setShadeHead(1);   //有帘头
							list.get(0).setPrice((Double) beizhe.get("zhePrice"));
							break;
						}
					
					Map<String, Object> zhubu = Calculate.clientPrice(
						(Double) beizhe.get("times_fold"),
						(Double) beizhe.get("hem"), 
						goods.getClothWide(), 
						goods.getClothHigh(), list.get(0));
					//设置商品布价格
					
					goods.setSumBPrice((Double) zhubu.get("sumPrice"));
					
					sumPrice = sumPrice + (Double) zhubu.get("sumPrice");
					// 帘头价格
					if (goods.getShadeHead() == 1) {
						Double Lprice = priceDao.selectLian(goods.getSampleId());
						//设置商品帘头价格
						goods.setSumLPrice(Lprice * (goods.getClothWide() * (Double) beizhe.get("times_fold")));
						sumPrice = sumPrice + Lprice * (goods.getClothWide() * (Double) beizhe.get("times_fold"));
					}
					// 遮光里布的宽
					Double zheguanglibu = (Double) zhubu.get("actualWide");
					if (list.get(0).getBreadthStatus() == 0 && (Integer) zhubu.get("useInverte") == 1
							|| list.get(0).getBreadthStatus() == 1 && (Integer) zhubu.get("useInverte") == 0) {
						Double shupin = priceDao.selectshupinsum(goods.getSampleId()) == null ? 0.0 : priceDao.selectshupinsum(goods.getSampleId());
						zheguanglibu = zheguanglibu + shupin;
					}
					sumPrice = calcShadingCloth(goods,sumPrice,beizhe,zheguanglibu);
					goods.setGoodsPrice(sumPrice);
					break;
			case 1:// 单纱
				Composition composition = priceDao.selectSha(goods.getSampleId());
				Map<String, Object> map = Calculate.clientPrice((Double) beizhe.get("times_fold"),
						(Double) beizhe.get("hem"), goods.getYarnWide(), goods.getYarnHigh(), composition);
				//设置商品纱价格
				goods.setSumSPrice((Double) map.get("sumPrice"));
				goods.setGoodsPrice((Double) map.get("sumPrice"));
				break;
			case 2:// 布加纱
				Double sumPrice2 = 0.0;
				List<Composition> list2 = priceDao.selectZhubu(goods.getSampleId());
				switch (goods.getStyle()) {
				case 0:// 打孔
					goods.setShadeHead(0);
					// 得到打孔价格
					list2.get(0).setPrice((Double) beizhe.get("holePrice"));
					break;
				case 1:// 韩折
					goods.setShadeHead(0);
					list2.get(0).setPrice((Double) beizhe.get("zhePrice"));
					break;
				case 2:// 韩折有帘头
					goods.setShadeHead(1);
					list2.get(0).setPrice((Double) beizhe.get("zhePrice"));
					break;
				case 3://韩折款或帘头款
					list2.get(0).setPrice((Double)beizhe.get("zhePrice"));
					break;
				}
				// 帘头价格
				if (goods.getShadeHead() == 1) {
					Double Lprice = priceDao.selectLian(goods.getSampleId());
					goods.setSumLPrice(Arith.mul(Lprice, goods.getClothWide()));
					sumPrice2 = sumPrice2 + Arith.mul(Lprice, goods.getClothWide());
				}
				Map<String, Object> zhubu2 = Calculate.clientPrice((Double) beizhe.get("times_fold"),
						(Double) beizhe.get("hem"), goods.getClothWide(), goods.getClothHigh(), list2.get(0));
				//设置商品布总价
				goods.setSumBPrice((Double) zhubu2.get("sumPrice"));
				sumPrice2 = sumPrice2 + (Double) zhubu2.get("sumPrice");
				Double zheguanglibu2 = (Double) zhubu2.get("actualWide");
				if (list2.get(0).getBreadthStatus() == 0 && (Integer) zhubu2.get("useInverte") == 1
						|| list2.get(0).getBreadthStatus() == 1 && (Integer) zhubu2.get("useInverte") == 0) {
					Double shupin = priceDao.selectshupinsum(goods.getSampleId());
					zheguanglibu2 = zheguanglibu2 + shupin;
				}
				sumPrice2 = calcShadingCloth(goods,sumPrice2,beizhe,zheguanglibu2);
				Composition composition3 = priceDao.selectSha(goods.getSampleId());
				Map<String, Object> sha = Calculate.clientPrice((Double) beizhe.get("times_fold"),
						(Double) beizhe.get("hem"), goods.getYarnWide(), goods.getYarnHigh(), composition3);
				logger.error(sha);
				goods.setSumSPrice((Double) sha.get("sumPrice"));
				sumPrice2 = sumPrice2 + (Double) sha.get("sumPrice");
				goods.setGoodsPrice(sumPrice2);
			}
		buluResult.setStatus(0);
		buluResult.setData(goods);
		return buluResult;
	}

	private double calcShadingCloth(Goods goods,double sumPrice,Map<String, Object> beizhe,Double zheguanglibu){
		if (goods.getShadingCloth() == 1) {//全遮光
			Composition composition2 = priceDao.selectZhe();
			Map<String, Object> zheguang = Calculate.clientPrice(1.0, (Double) beizhe.get("hem"), zheguanglibu,
					goods.getClothHigh(), composition2);
			//设置商品遮光里布总价
			goods.setSumShadingBprice((Double) zheguang.get("sumPrice"));
			sumPrice = sumPrice + (Double) zheguang.get("sumPrice");
		}
		if (goods.getShadingCloth() == 2) {//半遮光
			Composition composition2 = priceDao.selectBanzhe();
			Map<String, Object> banzhe = Calculate.clientPrice(1.0, (Double) beizhe.get("hem"), zheguanglibu,
					goods.getClothHigh(), composition2);
			//设置商品遮光里布总价
			goods.setSumShadingBprice((Double) banzhe.get("sumPrice"));
			sumPrice = sumPrice + (Double) banzhe.get("sumPrice");
		}
		return sumPrice;
	}
}
